<!--
  智慧社区管理系统 - 安防设备管理页面
  @author Wu.Liang
  @date 2025-01-20
  @description 安防设备监控、管理、维护
-->
<template>
  <div class="security-device-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-title">
          <el-icon size="24" class="title-icon"><VideoCamera /></el-icon>
          <h2>安防设备管理</h2>
        </div>
        <div class="header-actions">
          <el-button type="primary" @click="handleAddDevice">
            <el-icon><Plus /></el-icon>
            新增设备
          </el-button>
          <el-button @click="handleRefresh">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="statistics-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card total-devices">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Monitor /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.totalDevices }}</div>
                <div class="stat-label">设备总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card online-devices">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Monitor /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.onlineDevices }}</div>
                <div class="stat-label">在线设备</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card offline-devices">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Warning /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.offlineDevices }}</div>
                <div class="stat-label">离线设备</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card maintenance-devices">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Tools /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.maintenanceDevices }}</div>
                <div class="stat-label">维护中</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true" class="search-form">
        <el-form-item label="设备名称">
          <el-input
            v-model="searchForm.deviceName"
            placeholder="请输入设备名称"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="设备类型">
          <el-select
            v-model="searchForm.deviceType"
            placeholder="请选择设备类型"
            clearable
            style="width: 150px"
          >
            <el-option label="监控摄像头" :value="1" />
            <el-option label="门禁设备" :value="2" />
            <el-option label="报警器" :value="3" />
            <el-option label="对讲设备" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-select
            v-model="searchForm.deviceStatus"
            placeholder="请选择设备状态"
            clearable
            style="width: 150px"
          >
            <el-option label="在线" :value="1" />
            <el-option label="离线" :value="0" />
            <el-option label="维护中" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="安装位置">
          <el-input
            v-model="searchForm.location"
            placeholder="请输入安装位置"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 设备列表 -->
    <el-card class="device-list-card">
      <template #header>
        <div class="card-header">
          <span>设备列表</span>
          <div class="header-actions">
                         <el-button type="success" size="small" @click="handleBatchOnline">
               <el-icon><Monitor /></el-icon>
               批量上线
             </el-button>
            <el-button type="warning" size="small" @click="handleBatchMaintenance">
              <el-icon><Tools /></el-icon>
              批量维护
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="deviceList"
        v-loading="loading"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="deviceName" label="设备名称" min-width="150">
          <template #default="{ row }">
            <div class="device-name">
              <el-icon class="device-icon"><Monitor /></el-icon>
              <span>{{ row.deviceName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="deviceType" label="设备类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getDeviceTypeTag(row.deviceType)">
              {{ getDeviceTypeName(row.deviceType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="deviceStatus" label="设备状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getDeviceStatusTag(row.deviceStatus)">
              {{ getDeviceStatusName(row.deviceStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="location" label="安装位置" min-width="150" />
        <el-table-column prop="ipAddress" label="IP地址" width="120" />
        <el-table-column prop="lastOnlineTime" label="最后在线时间" width="180" />
        <el-table-column prop="signalStrength" label="信号强度" width="120">
          <template #default="{ row }">
            <el-progress
              :percentage="row.signalStrength"
              :color="getSignalColor(row.signalStrength)"
              :show-text="false"
              style="width: 80px"
            />
            <span style="margin-left: 8px">{{ row.signalStrength }}%</span>
          </template>
        </el-table-column>
        <el-table-column prop="maintenanceStatus" label="维护状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getMaintenanceStatusTag(row.maintenanceStatus)">
              {{ getMaintenanceStatusName(row.maintenanceStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              查看
            </el-button>
            <el-button type="warning" size="small" @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 设备详情弹窗 -->
    <el-dialog
      v-model="deviceDetailVisible"
      title="设备详情"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="设备名称">{{ currentDevice.deviceName }}</el-descriptions-item>
        <el-descriptions-item label="设备类型">{{ getDeviceTypeName(currentDevice.deviceType) }}</el-descriptions-item>
        <el-descriptions-item label="设备状态">
          <el-tag :type="getDeviceStatusTag(currentDevice.deviceStatus)">
            {{ getDeviceStatusName(currentDevice.deviceStatus) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="安装位置">{{ currentDevice.location }}</el-descriptions-item>
        <el-descriptions-item label="IP地址">{{ currentDevice.ipAddress }}</el-descriptions-item>
        <el-descriptions-item label="MAC地址">{{ currentDevice.macAddress }}</el-descriptions-item>
        <el-descriptions-item label="设备型号">{{ currentDevice.model }}</el-descriptions-item>
        <el-descriptions-item label="固件版本">{{ currentDevice.firmwareVersion }}</el-descriptions-item>
        <el-descriptions-item label="安装时间">{{ currentDevice.installTime }}</el-descriptions-item>
        <el-descriptions-item label="最后在线时间">{{ currentDevice.lastOnlineTime }}</el-descriptions-item>
        <el-descriptions-item label="信号强度">
          <el-progress
            :percentage="currentDevice.signalStrength"
            :color="getSignalColor(currentDevice.signalStrength)"
            style="width: 120px"
          />
        </el-descriptions-item>
        <el-descriptions-item label="维护状态">
          <el-tag :type="getMaintenanceStatusTag(currentDevice.maintenanceStatus)">
            {{ getMaintenanceStatusName(currentDevice.maintenanceStatus) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ currentDevice.remark || '暂无备注' }}</el-descriptions-item>
      </el-descriptions>

      <template #footer>
        <el-button @click="deviceDetailVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleEdit(currentDevice)">编辑设备</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { showMessage } from '@/utils/message'
import {
  VideoCamera,
  Plus,
  Refresh,
  Monitor,
  Warning,
  Tools,
  Search,
  View,
  Edit,
  Delete
} from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const deviceDetailVisible = ref(false)
const selectedDevices = ref<any[]>([])

// 搜索表单
const searchForm = reactive({
  deviceName: '',
  deviceType: undefined as number | undefined,
  deviceStatus: undefined as number | undefined,
  location: ''
})

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 20,
  total: 0
})

// 统计数据
const statistics = reactive({
  totalDevices: 156,
  onlineDevices: 142,
  offlineDevices: 8,
  maintenanceDevices: 6
})

// 当前设备详情
const currentDevice = ref<any>({})

// 设备列表数据
const deviceList = ref([
  {
    id: 1,
    deviceName: '前门监控摄像头',
    deviceType: 1,
    deviceStatus: 1,
    location: '小区前门',
    ipAddress: '192.168.1.100',
    lastOnlineTime: '2025-01-20 15:30:25',
    signalStrength: 95,
    maintenanceStatus: 1,
    macAddress: '00:11:22:33:44:55',
    model: 'IPC-HDW4431C-A',
    firmwareVersion: 'V2.1.0',
    installTime: '2024-06-15 10:00:00',
    remark: '高清夜视监控摄像头'
  },
  {
    id: 2,
    deviceName: '1号楼门禁',
    deviceType: 2,
    deviceStatus: 1,
    location: '1号楼单元门',
    ipAddress: '192.168.1.101',
    lastOnlineTime: '2025-01-20 15:28:10',
    signalStrength: 88,
    maintenanceStatus: 1,
    macAddress: '00:11:22:33:44:56',
    model: 'DS-K1T671M',
    firmwareVersion: 'V1.5.2',
    installTime: '2024-07-20 14:30:00',
    remark: '人脸识别门禁设备'
  },
  {
    id: 3,
    deviceName: '地下车库报警器',
    deviceType: 3,
    deviceStatus: 0,
    location: '地下车库',
    ipAddress: '192.168.1.102',
    lastOnlineTime: '2025-01-20 14:15:30',
    signalStrength: 45,
    maintenanceStatus: 2,
    macAddress: '00:11:22:33:44:57',
    model: 'DS-2CD2T47G1-L',
    firmwareVersion: 'V2.0.1',
    installTime: '2024-08-10 09:15:00',
    remark: '红外报警器，离线状态'
  },
  {
    id: 4,
    deviceName: '物业办公室对讲',
    deviceType: 4,
    deviceStatus: 1,
    location: '物业办公室',
    ipAddress: '192.168.1.103',
    lastOnlineTime: '2025-01-20 15:25:45',
    signalStrength: 92,
    maintenanceStatus: 1,
    macAddress: '00:11:22:33:44:58',
    model: 'DS-K5671-U',
    firmwareVersion: 'V1.8.0',
    installTime: '2024-09-05 16:20:00',
    remark: '可视对讲设备'
  },
  {
    id: 5,
    deviceName: '后门监控摄像头',
    deviceType: 1,
    deviceStatus: 2,
    location: '小区后门',
    ipAddress: '192.168.1.104',
    lastOnlineTime: '2025-01-20 12:30:15',
    signalStrength: 0,
    maintenanceStatus: 2,
    macAddress: '00:11:22:33:44:59',
    model: 'IPC-HDW4431C-A',
    firmwareVersion: 'V2.1.0',
    installTime: '2024-06-20 11:00:00',
    remark: '维护中，摄像头故障'
  }
])

// 获取设备类型标签样式
const getDeviceTypeTag = (type: number): 'primary' | 'success' | 'warning' | 'info' => {
  const typeMap: Record<number, 'primary' | 'success' | 'warning' | 'info'> = {
    1: 'primary',   // 监控摄像头
    2: 'success',   // 门禁设备
    3: 'warning',   // 报警器
    4: 'info'       // 对讲设备
  }
  return typeMap[type] || 'info'
}

// 获取设备类型名称
const getDeviceTypeName = (type: number): string => {
  const typeMap: Record<number, string> = {
    1: '监控摄像头',
    2: '门禁设备',
    3: '报警器',
    4: '对讲设备'
  }
  return typeMap[type] || '未知类型'
}

// 获取设备状态标签样式
const getDeviceStatusTag = (status: number): 'success' | 'danger' | 'warning' => {
  const statusMap: Record<number, 'success' | 'danger' | 'warning'> = {
    1: 'success',   // 在线
    0: 'danger',    // 离线
    2: 'warning'    // 维护中
  }
  return statusMap[status] || 'danger'
}

// 获取设备状态名称
const getDeviceStatusName = (status: number): string => {
  const statusMap: Record<number, string> = {
    1: '在线',
    0: '离线',
    2: '维护中'
  }
  return statusMap[status] || '未知状态'
}

// 获取维护状态标签样式
const getMaintenanceStatusTag = (status: number): 'success' | 'warning' | 'danger' => {
  const statusMap: Record<number, 'success' | 'warning' | 'danger'> = {
    1: 'success',   // 正常
    2: 'warning',   // 维护中
    0: 'danger'     // 故障
  }
  return statusMap[status] || 'danger'
}

// 获取维护状态名称
const getMaintenanceStatusName = (status: number): string => {
  const statusMap: Record<number, string> = {
    1: '正常',
    2: '维护中',
    0: '故障'
  }
  return statusMap[status] || '未知状态'
}

// 获取信号强度颜色
const getSignalColor = (strength: number): string => {
  if (strength >= 80) return '#67C23A'
  if (strength >= 60) return '#E6A23C'
  return '#F56C6C'
}

// 搜索处理
const handleSearch = () => {
  pagination.currentPage = 1
  loadDeviceList()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    deviceName: '',
    deviceType: undefined,
    deviceStatus: undefined,
    location: ''
  })
  handleSearch()
}

// 加载设备列表
const loadDeviceList = () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    loading.value = false
    pagination.total = deviceList.value.length
  }, 500)
}

// 新增设备
const handleAddDevice = () => {
  showMessage({ type: 'info', message: '新增设备功能开发中' })
}

// 刷新
const handleRefresh = () => {
  loadDeviceList()
  showMessage({ type: 'success', message: '数据已刷新' })
}

// 查看设备详情
const handleView = (row: any) => {
  currentDevice.value = { ...row }
  deviceDetailVisible.value = true
}

// 编辑设备
const handleEdit = (row: any) => {
  showMessage({ type: 'info', message: '编辑设备功能开发中' })
}

// 删除设备
const handleDelete = (row: any) => {
  showMessage({ type: 'info', message: '删除设备功能开发中' })
}

// 批量上线
const handleBatchOnline = () => {
  if (selectedDevices.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要操作的设备' })
    return
  }
  showMessage({ type: 'info', message: '批量上线功能开发中' })
}

// 批量维护
const handleBatchMaintenance = () => {
  if (selectedDevices.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要操作的设备' })
    return
  }
  showMessage({ type: 'info', message: '批量维护功能开发中' })
}

// 选择变化处理
const handleSelectionChange = (selection: any[]) => {
  selectedDevices.value = selection
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.currentPage = 1
  loadDeviceList()
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.currentPage = page
  loadDeviceList()
}

// 页面初始化
onMounted(() => {
  loadDeviceList()
})
</script>

<style scoped lang="scss">
.security-device-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 84px);

  .page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-title {
        display: flex;
        align-items: center;
        color: white;

        .title-icon {
          margin-right: 12px;
        }

        h2 {
          margin: 0;
          font-size: 24px;
          font-weight: 600;
        }
      }

      .header-actions {
        display: flex;
        gap: 12px;
      }
    }
  }

  .statistics-cards {
    margin-bottom: 20px;

    .stat-card {
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease;

      &:hover {
        transform: translateY(-2px);
      }

      .stat-content {
        display: flex;
        align-items: center;
        padding: 8px;

        .stat-icon {
          margin-right: 16px;
          padding: 12px;
          border-radius: 8px;
          color: white;
        }

        .stat-info {
          .stat-number {
            font-size: 28px;
            font-weight: bold;
            line-height: 1;
            margin-bottom: 4px;
          }

          .stat-label {
            font-size: 14px;
            color: #666;
          }
        }
      }

      &.total-devices .stat-icon {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      }

      &.online-devices .stat-icon {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }

      &.offline-devices .stat-icon {
        background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
      }

      &.maintenance-devices .stat-icon {
        background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
      }
    }
  }

  .search-card {
    margin-bottom: 20px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .search-form {
      .el-form-item {
        margin-bottom: 16px;
      }
    }
  }

  .device-list-card {
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: 600;
      font-size: 16px;

      .header-actions {
        display: flex;
        gap: 8px;
      }
    }

    .device-name {
      display: flex;
      align-items: center;

      .device-icon {
        margin-right: 8px;
        color: #409eff;
      }
    }

    .pagination-container {
      display: flex;
      justify-content: center;
      margin-top: 20px;
      padding-top: 20px;
      border-top: 1px solid #ebeef5;
    }
  }
}
</style> 